package edu.cmu.ece.ds.droidchat.preference;

import android.content.Context;
import android.graphics.Color;
import android.preference.Preference;
import android.preference.PreferenceManager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import edu.cmu.ece.ds.droidchat.R;
import edu.cmu.ece.ds.droidchat.model.MessageBackgroundColor;
import edu.cmu.ece.ds.droidchat.model.StringConstants;
import edu.cmu.ece.ds.droidchat.model.UserInfo;
import edu.cmu.ece.ds.droidchat.util.Toolbox;
import edu.cmu.ece.ds.droidchat.widget.ActionItem;
import edu.cmu.ece.ds.droidchat.widget.QuickActionList;
import edu.cmu.ece.ds.droidchat.widget.QuickActionList.OnListClickListener;

public class ColorPreference extends Preference {

    private static final String TAG = "ColorPreference";

    private TextView mPreviewTextView;
    private QuickActionList quickActionlist;

    public ColorPreference(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public ColorPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ColorPreference(Context context) {
        super(context);
    }

    @Override
    protected View onCreateView(ViewGroup parent) {

        final LayoutInflater layoutInflater = (LayoutInflater) getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        final LinearLayout layout = (LinearLayout) layoutInflater.inflate(
                R.layout.preference, parent, false);

        // the title
        TextView title = (TextView) layout.findViewById(android.R.id.title);
        title.setText(getTitle());

        // the summary
        TextView summary = (TextView) layout.findViewById(android.R.id.summary);
        summary.setText(getSummary());

        // The preview
        mPreviewTextView = new TextView(getContext());
        mPreviewTextView
                .setBackgroundResource(getMyBackgroundDrawableResId(Toolbox
                        .getMyInfo().getMessageBackgroundColor()));
        mPreviewTextView.setText(R.string.demo_text);
        mPreviewTextView.setTextColor(Color.BLACK);
        mPreviewTextView.setLayoutParams(new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT));
        mPreviewTextView.setFocusable(false);

        // the widget frame
        ViewGroup gp = (ViewGroup) layout
                .findViewById(android.R.id.widget_frame);
        gp.addView(mPreviewTextView);

        // QuickActionList
        this.quickActionlist = prepareColorPickerQuickActionList(mPreviewTextView);
        layout.invalidate();
        return layout;
    }

    public MessageBackgroundColor getMyMessageBackgroundColor() {
        return MessageBackgroundColor.values()[PreferenceManager
                .getDefaultSharedPreferences(getContext()).getInt(
                        StringConstants.PREF_KEY_MY_MESSAGE_BACKGROUND_COLOR,
                        MessageBackgroundColor.AQUA.ordinal())];

    }

    public QuickActionList prepareColorPickerQuickActionList(TextView archor) {
        QuickActionList quickActionList = new QuickActionList(archor,
                QuickActionList.VERTICAL);
        Context c = archor.getContext();

        quickActionList.addActionItem(new ActionItem(c,
                R.drawable.preview_aqua, R.string.bg_aqua,
                ActionItem.HORIZONTAL));
        quickActionList.addActionItem(new ActionItem(c,
                R.drawable.preview_purple, R.string.bg_purple,
                ActionItem.HORIZONTAL));
        quickActionList.addActionItem(new ActionItem(c,
                R.drawable.preview_pink, R.string.bg_pink,
                ActionItem.HORIZONTAL));
        quickActionList.addActionItem(new ActionItem(c,
                R.drawable.preview_orange, R.string.bg_orange,
                ActionItem.HORIZONTAL));
        quickActionList.addActionItem(new ActionItem(c,
                R.drawable.preview_lemon, R.string.bg_lemon,
                ActionItem.HORIZONTAL));
        quickActionList.addActionItem(new ActionItem(c,
                R.drawable.preview_lime, R.string.bg_lime,
                ActionItem.HORIZONTAL));
        quickActionList.addActionItem(new ActionItem(c,
                R.drawable.preview_graphite, R.string.bg_graphite,
                ActionItem.HORIZONTAL));
        quickActionList.addActionItem(new ActionItem(c,
                R.drawable.preview_clear, R.string.bg_clear,
                ActionItem.HORIZONTAL));
        quickActionList.setOnListClickListener(new VerticalListOnClickListener(
                archor));

        return quickActionList;
    }

    @Override
    protected void onClick() {
        super.onClick();
        quickActionlist.show();
    }

    private class VerticalListOnClickListener implements OnListClickListener {
        private TextView mTextView;

        public VerticalListOnClickListener(TextView textView) {
            mTextView = textView;
        }

        @Override
        public void onQuickActionListClicked(View contentView, int position) {
            UserInfo myInfo = Toolbox.getMyInfo();
            if (myInfo != null) {
                Toolbox.getMyInfo().setMessageBackgroundColor(
                        MessageBackgroundColor.values()[position]);
            }
            PreferenceManager
                    .getDefaultSharedPreferences(getContext())
                    .edit()
                    .putInt(StringConstants.PREF_KEY_MY_MESSAGE_BACKGROUND_COLOR,
                            position).commit();
            // TODO: send update message to server and peers
            mTextView
                    .setBackgroundResource(getMyBackgroundDrawableResId(position));
            mTextView.invalidate();
        }
    }

    /** Get the current value of the color option */

    public static int getMyBackgroundDrawableResId(int backgroundColor) {
        return getMyBackgroundDrawableResId(MessageBackgroundColor.values()[backgroundColor]);
    }

    public static int getMyBackgroundDrawableResId(
            MessageBackgroundColor messageBackgroundColor) {
        int retValue = R.drawable.chat_box_aqua_right;
        switch (messageBackgroundColor) {
        case AQUA:
            break;
        case PURPLE:
            retValue = R.drawable.chat_box_purple_right;
            break;
        case PINK:
            retValue = R.drawable.chat_box_pink_right;
            break;
        case ORANGE:
            retValue = R.drawable.chat_box_orange_right;
            break;
        case LEMON:
            retValue = R.drawable.chat_box_lemon_right;
            break;
        case LIME:
            retValue = R.drawable.chat_box_lime_right;
            break;
        case GRAPHITE:
            retValue = R.drawable.chat_box_graphite_right;
            break;
        case CLEAR:
            retValue = R.drawable.chat_box_clear_right;
            break;
        }
        return retValue;
    }

    public static int getOthersBackgroundDrawableResId(int backgroundColor) {
        return getOthersBackgroundDrawableResId(MessageBackgroundColor.values()[backgroundColor]);
    }

    public static int getOthersBackgroundDrawableResId(
            MessageBackgroundColor messageBackgroundColor) {

        int retValue = R.drawable.chat_box_aqua_left;
        switch (messageBackgroundColor) {
        case AQUA:
            break;
        case PURPLE:
            retValue = R.drawable.chat_box_purple_left;
            break;
        case PINK:
            retValue = R.drawable.chat_box_pink_left;
            break;
        case ORANGE:
            retValue = R.drawable.chat_box_orange_left;
            break;
        case LEMON:
            retValue = R.drawable.chat_box_lemon_left;
            break;
        case LIME:
            retValue = R.drawable.chat_box_lime_left;
            break;
        case GRAPHITE:
            retValue = R.drawable.chat_box_graphite_left;
            break;
        case CLEAR:
            retValue = R.drawable.chat_box_clear_left;
            break;
        }
        return retValue;
    }

}
